package com.android.zmusic.adapter;

import java.util.List;

import com.android.zmusic.musicplay.MusicData;
import com.android.zmusic.musicplay.MusicPlayState;
import com.android.zmusic.ui.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter {
	
	private LayoutInflater inflater;
	private List<MusicData> mMusicFileList;
	private int mCurPlayMusicIndex ;
	private int mPlayState;

	public ListViewAdapter(Context context,
			List<MusicData> mMusicFileList) {
		
		this.inflater = LayoutInflater.from(context);
		this.mMusicFileList = mMusicFileList;
		this.mCurPlayMusicIndex = -1;
		this.mPlayState = MusicPlayState.MP3_PREPARED;
	}
	
	public void refreshAdapter(List<MusicData> mMusicFileList) {
		this.mMusicFileList = mMusicFileList;
		notifyDataSetChanged();
	}
	
	public void setPlayState(int mCurPlayMusicIndex , int mPlayState) {
		this.mCurPlayMusicIndex = mCurPlayMusicIndex;
		this.mPlayState = mPlayState;
		notifyDataSetChanged();
	}
	
	

	public int getmCurPlayMusicIndex() {
		return mCurPlayMusicIndex;
	}

	public int getmPlayState() {
		return mPlayState;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mMusicFileList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(null== convertView ) {
			convertView = inflater.inflate(R.layout.listview_item, null);
		}
		
		showPlayStaticIcon(position,convertView);
		
		TextView posTextView = (TextView) convertView.findViewById(R.id.musiclistPos);
		posTextView.setText(String.valueOf(position + 1) + "." );
		
		TextView nameTextVIew = (TextView) convertView.findViewById(R.id.musicName);
		nameTextVIew.setText(mMusicFileList.get(position).mMusicName);
		
		TextView timeTextView = (TextView) convertView.findViewById(R.id.musicTime);
		int time = mMusicFileList.get(position).mMusicTime;
		timeTextView.setText(timeFormat(time));
		
		TextView aritstTextView = (TextView) convertView.findViewById(R.id.musicAritst);
		aritstTextView.setText(mMusicFileList.get(position).mMusicAritst);
		
		return convertView;
	}
	
	
    /**
     * 格式化时间
     * @param time
     * @return
     */
	private String timeFormat(int time) {
		int min = time /(1000 * 60);
		String sec = time % (1000 * 60) + "";
		if(sec.length() < 2) {
			sec += "00";
		}
		return min  + ":" + sec.trim().substring(0, 2);
	}

	/**
	 * 显示handle上的播放按钮
	 * @param position
	 * @param convertView
	 */
	private void showPlayStaticIcon(int position, View convertView) {
		
		ImageView imageView = (ImageView) convertView.findViewById(R.id.musicplaystate);
		
		if(position != mCurPlayMusicIndex) {
			//不是当前播放的音乐
			imageView.setVisibility(View.GONE);
			return ;
		}
		
		imageView.setVisibility(View.VISIBLE);
		if(mPlayState == MusicPlayState.MP3_PAUSE) {
			//当前播放状态为暂停
			imageView.setBackgroundResource(R.drawable.list_pause_icon);
		}else {
			//音乐播放中
			imageView.setBackgroundResource(R.drawable.list_play_icon);
		}
			
	}

}
